package org.wattdepot.cli.commands;

import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.List;
import javax.xml.datatype.XMLGregorianCalendar;
import org.junit.Test;
import org.wattdepot.cli.CommandLineInterface;
import org.wattdepot.client.WattDepotClient;
import org.wattdepot.util.tstamp.Tstamp;

/**
 * Test case for the CarbonContentCommand.
 * 
 * @author Paul Galiza
 * 
 */
public class TestCarbonContentCommand {
  private CommandLineInterface cli = new CommandLineInterface();
  private WattDepotClient client = cli.getClient();
  private CarbonContentCommand carbonContent = new CarbonContentCommand();

  /**
   * Tests the carbon calculation.
   * @throws Exception Exception.
   */
  @Test
  public void testCarbonContentCommand() throws Exception {
    
    List<String> userInput = new ArrayList<String>();
    userInput.add("");
    userInput.add("SIM_KAHE");
    userInput.add("2009-11-15T12:00:00.000-10:00");
    userInput.add("");
    userInput.add("10");
    
    XMLGregorianCalendar start = Tstamp.makeTimestamp(userInput.get(2));
    XMLGregorianCalendar end = Tstamp.incrementDays(start, 1);
    
    double carbonGenerated = client.getCarbonEmitted(userInput.get(1), start, 
        end, Integer.parseInt(userInput.get(4)));
    double energyGenerated = client.getEnergyGenerated(userInput.get(1), start, 
        end, Integer.parseInt(userInput.get(4)));

    double totalCarbon = carbonGenerated / energyGenerated;
    double carbonFinal = totalCarbon * 1000000;

    String expected = Math.round(carbonFinal) + " lbs CO2/MWh";
    String actual = carbonContent.doCommand(userInput, client);
    
    assertEquals("Testing source validity!", expected, actual);
    
  }

  /**
   * Test to see if the validation checker works.
   * 
   * @throws Exception NotAuthorizedException, ResourceNotFoundException, BadXmlException,
   * MiscClientException.
   */
  @Test
  public void validityTest() throws Exception {
    List<String> userInput = new ArrayList<String>();
    userInput.add("");
    userInput.add("Test_Source");// invalid source
    userInput.add("");
    userInput.add("2009-11-01");

    String actual = carbonContent.doCommand(userInput, client);
    assertEquals("Testing source validity!", "Invalid source.", actual);
    
    userInput.add(1, "SIM_KAHE");
    userInput.add(3, "1");
    actual = carbonContent.doCommand(userInput, client);
    assertEquals("Testing source validity!", "ERROR: Timestamp error.", actual);
    
  }
}
